package com.zksk.admin.controller.sale;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zksk.admin.api.domain.SysDictData;
import com.zksk.admin.domain.market.dto.SaleMemberDto;
import com.zksk.admin.domain.market.dto.SaleRecordDto;
import com.zksk.admin.domain.market.vo.SaleCardVo;
import com.zksk.admin.domain.market.vo.SaleMemberRequestVo;
import com.zksk.admin.domain.market.vo.SaleRecordRequestVo;
import com.zksk.admin.domain.member.vo.MemberVo;
import com.zksk.admin.service.market.SaleRecordService;
import com.zksk.admin.service.member.MarketMemberService;
import com.zksk.admin.service.member.MemberService;
import com.zksk.admin.service.system.ISysDictTypeService;
import com.zksk.common.core.constant.AdminConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.SpringUtils;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.SaleRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销列表 信息操作处理
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/market")
@Api(value = "MarketingController", tags = "营销相关接口")
public class MarketingController {

    @Autowired
    private MarketMemberService marketMemberService;

    @Autowired
    private SaleRecordService saleRecordService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ISysDictTypeService dictTypeService;


    @GetMapping("/dict/memberLevel")
    @Log(title = "查询企业类型", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询企业类型-数据字典")
    public R<List<SysDictData>> selectMemberType() {
        return R.ok(dictTypeService.selectDictDataByType(AdminConstants.MEMBER_LEVEL));
    }

    /**
     * 获取营销列表
     * @param requestVo
     * @return
     */
    @Log(title = "获取营销列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取营销列表")
    @PostMapping("/member/list")
    public R<IPage<SaleMemberDto>> list(@RequestBody SaleMemberRequestVo requestVo){
        // 获取当前登录用户角色
        // 超级管理员查看所有列表
        IPage<SaleMemberDto> page = marketMemberService.marketingMemberList(requestVo); // 分配得营销列表
//        //实体转Vo
//        BeanUtil.copyProperties(page, voPage);
        // 渠道管理员查看渠道下所有列表 后期做
        // 营销人员只能查看自己分配得营销记录
        return R.ok(page);
    }

    /**
     * 获取营销客户详情
     * @param memberId
     * @return
     */
    @GetMapping("/member/{memberId}")
    @Log(title = "获取营销客户详情", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据ID查询营销客户详情")
    public R<MemberVo> getById(@PathVariable("memberId") Long memberId) {
        return memberService.selectById(memberId);
    }


    /**
     * 新增/编辑营销记录
     * @param requestVo
     * @return
     */
    @Log(title = "新增/编辑营销记录", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增/编辑营销记录")
    @PostMapping("/record")
    public R<SaleRecord> save(@RequestBody SaleRecordRequestVo requestVo){
        String operatorName = requestVo.getOperatorName();
        String operatorPhone = requestVo.getOperatorPhone();
        Long memberId = requestVo.getMemberId();
        memberService.updateOperator(memberId,operatorName,operatorPhone);
        // 保存营销记录 对于卡片的判断
        SaleRecord record = saleRecordService.saveSaleRecord(requestVo);
        return R.ok(record, "保存营销记录成功");
    }


    /**
     * 查看营销记录卡片及其查看对应的营销记录
     * @param memberId
     * @return
     */
    @Log(title = "查看营销记录卡片及其查看对应的营销记录", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查看营销记录卡片及其查看对应的营销记录")
    @GetMapping("/record/card/{memberId}")
    public R<List<SaleCardVo>> getRecordCardAndCardList(@PathVariable(value = "memberId") Long memberId){
        List<SaleCardVo> saleCardVos = saleRecordService.cardList(memberId);
        return R.ok(saleCardVos,"获取营销卡片及其营销记录成功");
    }

    /**
     * 查看营销卡片对应得营销记录
     * @param productEnumId
     * @return
     */
    @Log(title = "查看营销卡片对应得营销记录", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查看营销卡片对应得营销记录")
    @GetMapping("/record/list/{memberId}")
    public R<List<SaleRecordDto>> cardRecordList(@PathVariable(value = "memberId") Long memberId,
                                                 @RequestParam(value = "productEnumId") Long productEnumId){
        List<SaleRecordDto> list = new ArrayList<>();
        List<SaleCardVo> saleCardVos = saleRecordService.cardList(memberId);


        for (SaleCardVo saleCardVo : saleCardVos) {
            if (saleCardVo.getProductEnumId().equals(productEnumId.toString())) {
                list = saleCardVo.getSaleRecordList();
                break;
            }
        }

        return R.ok(list, "获取营销记录成功");
    }

}
